// pages/xgzx/xgzx.js
Page({

  data: {
    navbar: ['单选题', '多选题', '判断题'],
    currentTab: 0,
    list:''
  },
    //顶部tab切换
    navbarTap: function (e) {
      console.log(e)
      this.setData({
        currentTab: e.currentTarget.dataset.idx
      })
    },
  onLoad(o) {
console.log(o)
let id= o.id
this.setData({
  id:id
})
wx.request({
  url: 'http://127.0.0.1:8080/zx/'+id,
  method:'GET',
  success: res => {
    console.log(res)
    this.setData({
      list:res.data.data,
      currentTab:res.data.data.tmlx-1
    })
  }
})
  },
  submit(e) {
    console.log(this.data.currentTab + 1)
    let leixing = this.data.currentTab + 1
    console.log(e.detail.value)
    let tiku = e.detail.value
    if (tiku.tmnr.length == 0) {
      wx.showToast({
        title: '题目不可为空',
        icon: 'error'
      })
      return
    }
    if (tiku.tmda1.length == 0) {
      wx.showToast({
        title: '前两个答题选项不可为空',
        icon: 'none'
      })
      return
    }
    if (tiku.tmda2.length == 0) {
      wx.showToast({
        title: '前两个答题选项不可为空',
        icon: 'none'
      })
      return
    }
    if (tiku.tmzqda.length == 0) {
      wx.showToast({
        title: '答案不可为空',
        icon: 'none'
      })
      return
    }
    if (tiku.tmdas.length == 0) {
      wx.showToast({
        title: '总共选项不可为空',
        icon: 'none'
      })
      return
    }
    let data = {
      tmda1: tiku.tmda1,
      tmda2: tiku.tmda2,
      tmda3: tiku.tmda3,
      tmda4: tiku.tmda4,
      tmda5: tiku.tmda5,
      tmda6: tiku.tmda6,
      tmnr: tiku.tmnr,
      tmzqda: tiku.tmzqda,
      tmzs: tiku.tmzs,
      tmdas: tiku.tmdas,
      tmlx:this.data.list.tmlx,
      fenlei_id:this.data.list.fenlei_id
    }
    console.log(data)
    let id = this.data.id
    wx.request({
      url: 'http://127.0.0.1:8080/zxup/'+id,
      method: 'PUT',
      data,
      success: res => {
        console.log(res)
        if (res.statusCode == 200) {
          wx.showToast({
            title: '修改成功',
            icon: 'success'
          })
          wx.navigateBack({         //返回上一页  
            delta:1,
            success: function (e) {
              var page = getCurrentPages().pop();
              if (page == undefined || page == null) return;
              page.onLoad(); // 刷新数据
            }
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
        }
      }
    })
  },

})